﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ValueConverters/NotNullConverter
* 类       名: NotNullConverter
* 功      能:  TD
* 时      间:  2024-04-27 15:25:00
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WaferSelectV3.ValueConverters
{
    public class NotNullConverter : OneWayConverter
    {
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool res = value != null;
            switch (value)
            {
                case ICollection collection:
                    res = collection.Count > 0;
                    break;
                case string str:
                    res = !string.IsNullOrEmpty(str);
                    break;
                case int i:
                    res = i != 0;
                    break;
                case double d:
                    res = Math.Abs(d) > 0.0001;
                    break;
            }

            // 看看是否需要反转,反转结果,根据parameter的值来判断
            if (((parameter is bool inverse) && inverse) || (parameter is string s && bool.TryParse(s, out inverse) && inverse))
                res = !res;

            if (targetType == typeof(Visibility))
                return res ? Visibility.Visible : Visibility.Collapsed;
            return res;
        }
    }
}
